//
//  VisitorView.swift
//  weibo27
//
//  Created by 董浩 on 16/4/19.
//  Copyright © 2016年 donghao. All rights reserved.
//

import UIKit

//定义访客视图协议
protocol visitorViewDelegate: NSObjectProtocol {


    //注册按钮协议方法
    func visitorViewDidRegister()
    //登录按钮协议方法
    func visitorViewDidLogin()
}

/// 处理用户未登录的视图页面
class VisitorView: UIView {
    
    //定义代理属性
    weak var delegate: visitorViewDelegate?
    
    //监听方法
    @objc private func clickRegister() {
    
    delegate?.visitorViewDidRegister()
    
    }
    
    @objc private func clickLogin() {
    
    delegate?.visitorViewDidLogin()
    }
    
    //设置视图方法
    func setInfo(imageName: String?,title: String) {
    
    messageLable.text = title
        guard let imageName = imageName else{
        
            startAnim()
        return
        
        }
    
        iconView.image = UIImage(named: imageName)
        homeIconView.hidden = true
        sendSubviewToBack(maskIconView)
    }
    //开启转轮动画的方法
    private func startAnim() {
    
    let anim = CABasicAnimation(keyPath: "transform.rotation")
      anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        

        iconView.layer.addAnimation(anim, forKey: nil)
    
    
    
    }

    //纯代码调用此方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    //SB调用此方法
    required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
        setupUI()
    }
    
    //懒加载控件
    //转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩图片
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息文字标签
    private lazy var messageLable: UILabel = {
    
    let messageLable = UILabel()
        
        messageLable.text = "关注一些人，回这里看看有什么惊喜"
        messageLable.textColor = UIColor.darkGrayColor()
        messageLable.font = UIFont.systemFontOfSize(14.0)
        messageLable.numberOfLines = 0
        messageLable.textAlignment = NSTextAlignment.Center
        return messageLable
    
    
    }()
    //注册按钮
    private lazy var registerBtn: UIButton = {
    
        let registerBtn = UIButton()
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    
    
    
        return registerBtn
    }()
    //登录按钮
    private lazy var loginBtn: UIButton = {
    let loginBtn = UIButton()
    loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    
    
    return loginBtn
    }()
}

extension VisitorView {


    //设置未登录界面视图UI
    private func setupUI() {
    //1，添加转轮
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
    
    //2，设置自动布局
        for v in subviews {
        v.translatesAutoresizingMaskIntoConstraints = false
        
        }
        //转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
    
        //小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //消息文字标签
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Left, relatedBy: .Equal, toItem: messageLable, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))


        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: messageLable, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //遮罩图片
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))


        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regBtn]", options: [], metrics: ["btnHeight":-36], views: ["regBtn":registerBtn,"mask":maskIconView]))
        //设置遮罩下边背景图
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        
        //给按钮添加监听方法
        registerBtn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
    
        loginBtn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
    }


}